package com.cliff.cloudnote.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cliff.cloudnote.bean.UserBean;
import com.cliff.cloudnote.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	private UserService userService;
	
	/**
	 * 用户注册
	 * @param userBean
	 * @param request
	 * @return
	 */
	@RequestMapping("/register")
	public void register(UserBean userBean, HttpServletRequest request, HttpServletResponse response) throws IOException {
//		if (userService.checkExistUser(userBean)) {
//			return "用户已存在";
//		}
//		userService.registerUser(userBean);
		response.sendRedirect("../index.html");
	}
	
	@RequestMapping("/login")
	public String login(UserBean userBean, HttpServletRequest request) {
		HttpSession session = request.getSession();
		session.setAttribute("username", userBean.getUsername());
		session.setAttribute("password", userBean.getPassword());
		if (userService.checkPassword(userBean)) {
			return "good";
		} else {
			return "failure";
		}
	}
	
	@RequestMapping("logout")
	public String logout() {
		return "";
	}
}
